//    Copyright (C) Kherty.  All rights reserved.
using System;

namespace OpenLS.Spreadsheet.StandardAddIn.Statistics
{
    internal class TDistribution : ContinuousDistribution
    {
        private double _degreesOfFreedom;

        public TDistribution(double degreesOfFreedom)
        {
            DegreesOfFreedom = degreesOfFreedom;
        }

        public double DegreesOfFreedom
        {
            get { return _degreesOfFreedom; }
            set
            {
                if (value <= 0.0)
                    throw new ArgumentOutOfRangeException("value");
                _degreesOfFreedom = value;
            }
        }

        public override double CumulativeProbability(double x)
        {
            if (x == 0)
                return 0.5;
            double t = Statistician.RegularizedBeta(
                DegreesOfFreedom/(DegreesOfFreedom + (x*x)), 0.5*DegreesOfFreedom, 0.5);
            if (x < 0)
                return 0.5*t;
            return 1 - 0.5*t;
        }

        public override double InverseCumulativeProbability(double probability)
        {
            if (probability == 0)
                return double.NegativeInfinity;
            if (probability == 1)
                return double.PositiveInfinity;
            double result = base.InverseCumulativeProbability(probability);
            return result;
        }

        protected override SolverParameters GetSolverParameters(double probability)
        {
            return new SolverParameters(
                double.MinValue,
                double.MaxValue,
                0);
        }
    }
}